package com.koudai.reader.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.koudai.reader.vo.Book;
import com.koudai.reader.vo.EndUser;

/**
 * Created by StarCite. User: andy.song Date: Apr 13, 2010 Time: 11:49:17 PM To
 * change this template use File | Settings | File Templates.
 */
public class Utils {

    public static final String Empty_Oid_Value = "NA";
    public static final String Empty_CorrelationId = "NA";
    public static final String Empty_Executed_Escalation_Type = "NA";
    public static final String LINE_SEPERATOR = System.getProperty("line.separator");

    protected static String hostAddress;

    private static String serverName;

    protected static String serverUniqueId;

    static Object parkObject = new Object();

    public static List<String> getChapterList(Book book) {
        List<String> list = new ArrayList<String>();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(Constant.DIR + book.getBookid() + "/charpter.txt"))));
            
            String line = null;
            while ((line = reader.readLine()) != null) {
                if (!line.trim().equals("")) {
                    list.add(line);
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        return list;
    }
    
    public static boolean isNullOrEmpty(Collection<?> val) {
        return val == null || val.isEmpty();
    }

    public static boolean isNullOrEmpty(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }

    public static String getLocalNetAddress() {
        if (hostAddress == null) {

            try {
                hostAddress = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                return "127.0.0.1";
            }
        }

        return hostAddress;
    }

    public static String getLocalServerName() {
        if (serverName == null) {
            try {
                serverName = InetAddress.getLocalHost().getHostName();
            } catch (Exception e) {
                return "";
            }
        }
        return serverName;
    }

    public static String getUniqueId() {
        return String.valueOf(System.nanoTime());
    }

    public static String outputStackTrace(Throwable ex) {
        String returnValue = "";

        ByteArrayOutputStream bos = null;
        PrintStream ps = null;
        try {
            bos = new ByteArrayOutputStream();
            ps = new PrintStream(bos, true);
            ex.printStackTrace(ps);

            ps.flush();
            bos.flush();

            returnValue = bos.toString();
        } catch (Exception ext) {
            // Swallow
        } finally {
            if (ps != null) {
                ps.close();
            }

            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                }
            }
        }

        return returnValue;
    }

    /**
     * Check-in for the string check from the null to the empty.
     * 
     * @param
     * */
    public static String nullToEmpty(String string) {
        if (string == null)
            return "";
        return string;
    }

    public static boolean isNullorEmpty(String str) {
        boolean result = false;

        if (str == null || "".equals(str.trim())) {
            result = true;
        }

        return result;
    }

    
    public static int convertToScreenPixels(int dipPixels) {
        
        return (int) convertToScreenPixels((double) dipPixels);
    }
    
    public static double convertToScreenPixels(double dipPixels) {
        return EndUser.getInstance().density > 0.0D ? dipPixels * EndUser.getInstance().density : dipPixels;
    }
    
    /*
     * Ȼ����gunzip��ѹ
     */
    public static String guzipCompress(InputStream inputStream) {
        if (inputStream == null) {
            return null;
        }
        
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {
            GZIPInputStream gzip = new GZIPInputStream(inputStream);
            byte[] buffer = new byte[256];
            int n = 0;
            while ((n = gzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            return out.toString("UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            
            return e.getMessage();
        }

        return null;
    }
    
    public static boolean hasNetwork (Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni != null) {

            return true;
        } else {
            return false;
        }
    }
}
